/**
 * @update : 2013/04/09
 */

var Form = function(){
	
	this.addTr = function(tab, idTr){
		var html = '';
		if(tab == 'tab1'){
			html = '<tr class="'+idTr+'"><td>'+idTr+'</td><td><input type="text" value="" name="baibao[]" class="input-xlarge" maxlength="500"></td>';
			html += '<td><a href="#" onclick="form.deleteRow(\'tab1\','+idTr+');return false;" class="close">&times;</a></td></tr>';
		}
		if(tab == 'tab2'){
			html = '<tr class="'+idTr+'"><td>'+idTr+'</td><td><input type="text" value="" name="congtrinh[]" class="input-xlarge" maxlength="500"></td>';
			html += '<td><a href="#" onclick="form.deleteRow(\'tab2\','+idTr+');return false;" class="close">&times;</a></td></tr>';
		}
		if(tab == 'tab3'){
			html = '<tr class="'+idTr+'"><td>'+idTr+'</td><td><input type="text" value="" name="giaotrinh[]" class="input-xlarge" maxlength="500"></td>';
			html += '<td><a href="#" onclick="form.deleteRow(\'tab3\','+idTr+');return false;" class="close">&times;</a></td></tr>';
		}
		if(tab == 'tab4'){
			html = '<tr class="'+idTr+'">';
			html +=	'<td>'+idTr+'</td>';
			html +=	'<td><input type="text" value="" name="huongdan['+idTr+'][hoten]" class="input-medium" maxlength="100"></td>';
			html +=	'<td><input type="text" value="" name="huongdan['+idTr+'][detai]" class="input-medium" maxlength="255"></td>';
			html += '<td><input type="text" value="" name="huongdan['+idTr+'][from]" class="datepicker2 input-medium"></td>';
			html +=	'<td><input type="text" value="" name="huongdan['+idTr+'][to]" class="datepicker2 input-medium"></td>';
			html += '<td><a href="#" onclick="form.deleteRow(\'tab4\','+idTr+');return false;" class="close">&times;</a></td></tr>';
			html += '</tr>';
			html += '<script>';
			html += '$(document).ready(function(){';
			html += '$( ".datepicker2" ).datepicker({dateFormat: "dd/mm/yy",changeMonth: true,changeYear: true,inline: true});';
			html += '});';
			html += '</script>';
		}
		return html;
	}
	
	this.deleteRow = function(tab, idTr){
		$('#'+tab).find('tr[class="'+idTr+'"]').remove();
		this.orderNo(tab);
		return false;
	}
	
	this.addRow = function(tab){
		var trLast = $('#'+tab).find('tr:last');
		var trId = parseInt(trLast.prev().attr('class')) + 1;
		trLast.before(this.addTr(tab, trId));
		this.orderNo(tab);
		return false;
	}
	
	this.orderNo = function(tab){
		var trId = 0;
		$('#'+tab).find('tr[class]').each(function(){
			trId++;
			$(this).find('td:first').html(trId);
		});
	}
}